package xyz.luxiaoxiang.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.luxiaoxiang.bean.HotQuestion;
import xyz.luxiaoxiang.dao.HotQuestionDao;
import xyz.luxiaoxiang.service.TopicService;

/**
 * @author luxx
 * @date 2018/1/15 0015
 */
@RestController
public class TopicController {


    /**
     * 获取MainTopicList
     *
     * @return
     */
    @RequestMapping(value = "/getMainTopic", method = RequestMethod.GET)
    public String getMainTopic() {
        return JSONObject.toJSONString(topicService.getMainTopic());
    }

    /**
     * 刷新MainTopicList
     */
    @RequestMapping(value = "/refreshMainTopic", method = RequestMethod.GET)
    public String refreshMainTopic() {
        return JSONObject.toJSONString(topicService.refreshMainTopic());
    }

    /**
     * 获取ChildTopicList
     */
    @RequestMapping(value = "/getChildTopic", method = RequestMethod.GET)
    public String getChildTopic() {
        return JSONObject.toJSONString(topicService.getChildTopic());
    }

    /**
     * 刷新ChildTopicList
     */
    @RequestMapping(value = "/refreshChildTopic", method = RequestMethod.GET)
    public String refreshChildTopic() {
        topicService.refreshChildTopic();
        return "别再刷了";
    }

    /**
     * 刷新ChildTopic
     */
    @RequestMapping(value = "/refreshChildTopicById", method = RequestMethod.GET)
    public String refreshChildTopicById(@RequestParam(name = "main_topic_id") int mainTopicId) {
        return JSONObject.toJSONString(topicService.refreshChildTopic(mainTopicId));
    }

    /**
     * 获取ChildTopic
     */
    @RequestMapping(value = "/getChildTopicById", method = RequestMethod.GET)
    public String getChildTopicById(@RequestParam(name = "main_topic_id") int mainTopicId) {
        return JSONObject.toJSONString(topicService.getChildTopic(mainTopicId));
    }

    /**
     * 从本地获取热门回答
     */
    @RequestMapping(value = "/getHostQuestion", method = RequestMethod.GET)
    public String getHostQuestion() {
        return JSONObject.toJSONString(topicService.getHotQuestion());
    }

    /**
     * 从本地获取热门回答
     */
    @RequestMapping(value = "/getHostQuestionRemote", method = RequestMethod.GET)
    public String getHostQuestionRemote() {
        return JSONObject.toJSONString(topicService.getHotQuestionRemote());
    }

    /**
     * 从本地获取热门回答(单个话题)
     */
    @RequestMapping(value = "/getHostQuestionById", method = RequestMethod.GET)
    public String getHostQuestionById(@RequestParam(name = "child_topic_id") int childTopicId) {
        return JSONObject.toJSONString(topicService.getHotQuestion(childTopicId));
    }

    /**
     * 从本地获取热门回答(单个回答)
     */
    @RequestMapping(value = "/getAnswerById", method = RequestMethod.GET)
    public String getAnswerById(@RequestParam(name = "answer_id") int answerId) {
        HotQuestion hotQuestion = hotQuestionDao.findByAnswerId(answerId);
        if(hotQuestion == null){
            return "未找到相关信息";
        }
        return hotQuestion.getExcerpt();
    }

    /**
     * 从远程获取热门回答(单个话题)
     */
    @RequestMapping(value = "/getHostQuestionRemoteById", method = RequestMethod.GET)
    public String getHostQuestionRemoteById(@RequestParam(name = "child_topic_id") int childTopicId) {
        return topicService.getHotQuestionRemote(childTopicId).size() + "";
    }


    @Autowired
    private TopicService topicService;
    @Autowired
    private HotQuestionDao hotQuestionDao;
}
